Ext.onReady(function() {

	Ext.SSL_SECURE_URL = "../ext-3.2.1/resources/images/default/s.gif";
	Ext.BLANK_IMAGE_URL = "../ext-3.2.1/resources/images/default/s.gif";

	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
	var menuHeight = document.body.offsetHeight - 100;

	var showOnePanel = true;// 设置主页面单窗口还是多窗口

	var tools = [{
		id : 'refresh',
		qtip : '刷新',
		handler : function(e, target, panel) {
			if (panel.id == 'board') {
				// afficheGrid.store.reload();
			}
			if (panel.id == 'message') {
				// meetingNoticeGrid.store.reload();
			}
			if (panel.id == 'worklist') {

			}
			if (panel.id == 'info') {

			}
		}
	}

	];

	var centerTabPanel = new Ext.TabPanel({
		region : 'center',
		id : 'centerTabPanel',
		deferredRender : false,
		activeTab : 0,
		margins : '0 5 5 0',
		// plugins: new Ext.ux.TabCloseMenu(),
		resizeTabs : true,
		minTabWidth : 100,
		tabWidth : 150,
		enableTabScroll : true,
		autoScroll : true,
		items : [{
			xtype : 'portal',
			margins : '35 5 5 0',
			title : '首页',
			iconCls : 'tabs',
			autoScroll : true,
			items : [{
				columnWidth : .99,
				style : 'padding:0px 0 0px 0px',
				html : '<iframe scrolling="no" id="_iframe"  frameborder="0" width="100%" height="'
						+ ((Ext.isIE
								? (document.body.offsetHeight - 110)
								: (window.innerHeight - 140))) // 动态计算高度
						+ '"   src= "../pages/menu/menuList.jsp"></iframe>'
			}]
		}],
		loadPage : function(href, cls, title) {
			var tabId = 'workPanel' + cls;
			var tab = this.getComponent(tabId);
			if (showOnePanel) {
				tabId = 'workPanel';
				tab = this.getComponent(tabId);
				if (tab) {
					this.setActiveTab(tab);
					tab.setTitle(title);
					Ext.get('iframe-workPanel').dom.src = href;
				} else {
					var pIn1 = this.add(new DocPanel({
						id : tabId,
						cclass : cls,
						layout : 'fit',
						items : [new IFrameComponent({
							id : tabId,
							url : href
						})],
						title : title,
						iconCls : 'tabs'
					}));
					this.setActiveTab(pIn1);
				}
			} else {
				if (tab) {
					this.setActiveTab(tab);
				} else {
					var autoLoad = {
						url : href,
						scripts : true
					};
					var p = this.add(new DocPanel({
						id : tabId,
						cclass : cls,
						layout : 'fit',
						items : [new IFrameComponent({
							id : tabId,
							url : href
						})],
						title : title,
						iconCls : 'tabs'
					}));
					this.setActiveTab(p);
				}
			}
		},
		loadPageAlways : function(href, cls, title) {
			var tabId = 'workPanel' + cls;
			var tab = this.getComponent(tabId);
			if (tab) {
				this.setActiveTab(tab);
			} else {
				var autoLoad = {
					url : href,
					scripts : true
				};
				var p = this.add(new DocPanel({
					id : tabId,
					cclass : cls,
					layout : 'fit',
					closable : true,
					items : [new IFrameComponent({
						id : tabId,
						url : href
					})],
					title : title,
					iconCls : 'tabs'
				}));
				this.setActiveTab(p);
			}

		}

	});

	IFrameComponent = Ext.extend(Ext.BoxComponent, {
		onRender : function(ct, position) {
			this.el = ct.createChild({
				tag : 'iframe',
				id : 'iframe-' + this.id,
				frameBorder : 0,
				src : this.url
			});
		}
	});

	DocPanel = Ext.extend(Ext.Panel, {
		closable : true,
		autoScroll : true,
		initComponent : function() {
			DocPanel.superclass.initComponent.call(this);
		},
		scrollToMember : function(member) {
			var el = Ext.fly(this.cclass + '-' + member);
			if (el) {
				var top = (el.getOffsetsTo(this.body)[1])
						+ this.body.dom.scrollTop;
				this.body.scrollTo('top', top - 25, {
					duration : .75,
					callback : this.hlMember.createDelegate(this, [member])
				});
			}
		},
		scrollToSection : function(id) {
			var el = Ext.getDom(id);
			if (el) {
				var top = (Ext.fly(el).getOffsetsTo(this.body)[1])
						+ this.body.dom.scrollTop;
				this.body.scrollTo('top', top - 25, {
					duration : .5,
					callback : function() {
						Ext.fly(el).next('h2').pause(.2).highlight('#8DB2E3', {
							attr : 'color'
						});
					}
				});
			}
		},
		hlMember : function(member) {
			var el = Ext.fly(this.cclass + '-' + member);
			if (el) {
				el.up('tr').highlight('#cadaf9');
			}
		}
	});

	new Ext.Viewport({
		layout : 'border',
		frame : false,
		items : [new Ext.BoxComponent({ // raw
			region : 'north',
			el : 'north'
				// height:25
		}), {
			region : 'west',
			id : 'west-panel',
			header : false,
			split : true,
			width : 200,
			minSize : 175,
			frame : false,
			maxSize : 400,
			collapsible : true,
			margins : '0 0 5 5',
			layout : 'accordion',
			layoutConfig : {
				animate : true
			},
			items : [{
				title : '系统功能',
				border : false,
				frame : false,
				id : 'menu1',
				iconCls : 'nav',
				html : '<div id="tree-viewer" style="overflow:auto;height:'
						+ menuHeight + ';border:0px solid #c3daf9;"></div>'
			}

			]
		}, centerTabPanel]
	});

	Ext.Ajax.timeout = 90000;
	var tree;
	var root;
	if (!tree) {
		var Tree = Ext.tree;
		tree = new Tree.TreePanel({
			el : 'tree-viewer',
			autoScroll : true,
			animate : false,
			border : false,
			enableDD : false,
			containerScroll : true,
			rootVisible : false,
			draggable : false,
			lines : true,
			loader : new Tree.TreeLoader({
				dataUrl : '../menu/loadMenu.action'
			})
		});
		root = new Tree.AsyncTreeNode({
			text : '根节点',
			draggable : false,
			id : '0'
		});
		tree.setRootNode(root);

		tree.on('click', function(node, e) {
			e.stopEvent();
			if (node.isLeaf()) {
				document.all('currentToOpenMenuId').value = node.id;
				if (node.attributes.href == 'default.jsp?onlineUser') {
					onlineUserwin.show();
				} else
					centerTabPanel.loadPage(node.attributes.href, node.id,
							node.text);
			} else {
				node.toggle();
			}
		});
	}
	tree.on("load", function(node) {

	});
	tree.render();
	root.expand();

	function checkNode(node, uri) {
		var flag = false;
		if (!node.isLeaf()) {
			var childrenNodes = node.childNodes;
			for (var i = 0; i < childrenNodes.length; i++) {
				var nodeTemp = childrenNodes[i];
				flag = checkNode1(nodeTemp, uri)
			}
		} else {
			alert("")
			var hrefTemp = node.attributes.href;
			alert(hrefTemp)
			if (hrefTemp.indexOf(uri) != -1)
				flag = false;
			else
				flag = true;
		}
		return flag;
	}

	function checkNode1(node, uri) {
		var flag = false;
		if (!node.isLeaf()) {
			alert(node.attributes.href)
			var childrenNodes = node.childNodes;
			alert(childrenNodes.length)
			for (var i = 0; i < childrenNodes.length; i++) {
				var nodeTemp = childrenNodes[i];
				flag = checkNode(nodeTemp, uri)
			}
		} else {
			alert("")
			var hrefTemp = node.attributes.href;
			alert(hrefTemp)
			if (hrefTemp.indexOf(uri) != -1)
				flag = false;
			else
				flag = true;
		}
		return flag;
	}

	var userList = function() {
		onlineUserwin.show();
	}
	var changeTheme = function() {
		changeThemewin.show();
	}
	var changePassword = function() {
		changePasswordwin.show();
	}

	var changePwdHref = Ext.get('changePwdHref');
	changePwdHref.on('click', changePassword);

	Ext.apply(Ext.form.VTypes, {
		password : function(val, field) {
			if (field.initialPassField) {
				var pwd = Ext.getCmp(field.initialPassField);
				return (val == pwd.getValue());
			}
			return true;
		},
		passwordText : '两次输入的密码不一致'
	});
	var pass1 = new Ext.form.TextField({
		fieldLabel : '新密码',
		name : 'pass',
		allowBlank : false,
		width : 125,
		minLength : 6,
		inputType : 'password',
		id : 'pass'
	})
	var pass2 = new Ext.form.TextField({
		fieldLabel : '确认新密码',
		name : 'pass-cfrm',
		vtype : 'password',
		allowBlank : false,
		width : 125,
		minLength : 6,
		inputType : 'password',
		initialPassField : 'pass'
	})
	var changePasswordFormPanel;
	if (!changePasswordFormPanel) {
		changePasswordFormPanel = new Ext.FormPanel({
			labelWidth : 100,
			labelAlign : 'right',
			frame : true,
			bodyStyle : 'padding:55px',
			url : '../user/changePassword.action',
			items : [pass1, pass2]
		});
	}

	var changePasswordwin;
	if (!changePasswordwin) {
		changePasswordwin = new Ext.Window({
			title : '修改用户密码',
			id : 'changePasswordwin',
			closable : true,
			closeAction : 'hide',
			collapsible : true,
			resizable : false,
			width : 400,
			height : 300,
			layout : 'fit',
			plain : true,
			bodyStyle : 'padding:5px;',
			maximizable : false,
			buttonAlign : 'center',
			items : changePasswordFormPanel,
			buttons : [{
				text : '修改',
				handler : function() {
					if (changePasswordFormPanel.getForm().isValid()) {
						var ct = pass1.getValue();
						ct = ct.trim();
						if (ct.length == 0) {
							Ext.Msg.alert('提示', '请输入正确密码格式，不能使用全空格密码！');
							return;
						}
						changePasswordFormPanel.getForm().submit({
							waitMsg : '正在处理，请稍等...',
							reset : true,
							failure : function(form, action) {
								Ext.MessageBox.hide();
								Ext.Msg.show({
									title : '错误',
									msg : '<span style="text-align:center;width:250px;margin:5 0 5 0;">'
											+ action.result.msg + '</span>',
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.WARNING
								});
								changePasswordwin.hide();
							},
							success : function(form, action) {
								Ext.MessageBox.hide();
								Ext.Msg.show({
									title : '成功',
									msg : '<span style="text-align:center;width:250px;margin:5 0 5 0;">'
											+ action.result.msg + '</span>',
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.INFO
								});
								changePasswordwin.hide();
							},
							method : 'POST'
						});
					}
				}
			}, {
				handler : function() {
					changePasswordwin.hide();
				},
				text : '关闭'
			}]
		});
	}

	var path = window.location.pathname;
	path = path.substring(0, path.lastIndexOf('/'));
	path = path.substring(0, path.lastIndexOf('/') + 1);
	var themeStore = new Ext.data.SimpleStore({
		fields : ['value', 'label'],
		data : [[path + 'ext/resources/css/ext-all.css', '默认'],
				[path + 'resources/css/xtheme-darkgray.css', '暗灰'],
				[path + 'resources/css/xtheme-olive.css', '绿色'],
				[path + 'resources/css/xtheme-purple.css', '紫色'],
				[path + 'resources/css/xtheme-slate.css', '暗蓝']

		]
	});
	// [path + 'resources/css/xtheme-black.css', '黑色'],
	// [path + 'resources/css/xtheme-slickness.css', '黑白']
	var themeComboBox = new Ext.form.ComboBox({
		fieldLabel : '页面风格',
		store : themeStore,
		displayField : 'label',
		valueField : 'value',
		typeAhead : true,
		editable : false,
		mode : 'local',
		triggerAction : 'all',
		selectOnFocus : true,
		value : path + 'ext/resources/css/ext-all.css',
		width : 125
	});
	themeComboBox.on('select', function() {
		document.all('currentTheme').value = themeComboBox.value;
		Ext.util.CSS.swapStyleSheet('theme', themeComboBox.value);
	});
	var changeThemeFormPanel
	if (!changeThemeFormPanel) {
		changeThemeFormPanel = new Ext.FormPanel({
			labelWidth : 100,
			labelAlign : 'right',
			frame : true,
			bodyStyle : 'padding:65x;',
			items : [themeComboBox]
		});
	}
	var changeThemewin;
	if (!changeThemewin) {
		changeThemewin = new Ext.Window({
			title : '设置页面风格样式',
			id : 'changeThemewin',
			closable : true,
			closeAction : 'hide',
			collapsible : true,
			resizable : false,
			width : 400,
			height : 300,
			layout : 'fit',
			plain : true,
			bodyStyle : 'padding:5px;',
			maximizable : false,
			buttonAlign : 'center',
			items : changeThemeFormPanel,
			buttons : [{
				handler : function() {
					changeThemewin.hide();
				},
				text : '关闭'
			}]
		});
	}


});
